package org.chen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.chen.exception.ServiceException;
import org.chen.model.CommentFieldPattern;
import org.chen.request.BatchStatusRequest;
import org.chen.response.R;
import org.chen.service.impl.CommentFieldPatternServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/pattern")
@RequiredArgsConstructor
@Slf4j
public class RuleController {

    @Autowired
    private CommentFieldPatternServiceImpl patternService;

    @PostMapping
    public R<Void> addPattern(@RequestBody @Validated CommentFieldPattern pattern) {
        try {
            patternService.addPattern(pattern);
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public R<Void> updatePattern(
            @PathVariable Long id,
            @RequestBody @Validated CommentFieldPattern pattern) {
        try {
            pattern.setId(id);
            patternService.updatePattern(pattern);
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public R<Void> deletePattern(@PathVariable Long id) {
        try {
            patternService.deletePattern(id);
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/list")
    public R<IPage<CommentFieldPattern>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String fieldType) {
        try {
            Page<CommentFieldPattern> page = new Page<>(current, size);
            IPage<CommentFieldPattern> result = patternService.getPatternList(page, fieldType);
            return R.success(result);
        } catch (Exception e) {
            log.error("获取规则列表失败", e);
            return R.error("获取规则列表失败");
        }
    }

    @PostMapping("/refresh-cache")
    public R<Void> refreshCache() {
        try {
            patternService.refreshAllCache();
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/batch/delete")
    public R<Void> batchDelete(@RequestBody List<Long> ids) {
        try {
            patternService.batchDeletePatterns(ids);
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/batch/status")
    public R<Void> batchUpdateStatus(@RequestBody BatchStatusRequest request) {
        try {
            patternService.batchUpdateStatus(request.getIds(), request.getEnabled());
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }


}